#include "stdafx.h"
#include "BigInt.h"

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
using namespace APIAL;

namespace BigIntUnitTest
{
	[TestClass]
	public ref class MultiplyUnitTest
	{
	private:
		TestContext^ testContextInstance;
		static BigInt *op1;
		static BigInt *op2;
		static BigInt *scratch1;
		static BigInt *result1;
		static BigInt *result2;
		static BigInt *expected;

	public: 
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext
		{
			Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get()
			{
				return testContextInstance;
			}
			System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value)
			{
				testContextInstance = value;
			}
		};

		#pragma region Additional test attributes
		//
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		static void MyClassInitialize(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ testContext) {
			op1 = new BigInt(3, 8);
			op2 = new BigInt(3, 8);
			result1 = new BigInt(3, 8);
			result2 = new BigInt(3, 8);
			scratch1 = new BigInt(3, 8);
			expected = new BigInt(3, 8);
		}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		[ClassCleanup()]
		static void MyClassCleanup() { 
			delete op1;
			delete op2;
			delete result1;
			delete result2;
			delete scratch1;
			delete expected;
		}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//void MyTestInitialize() {};
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//void MyTestCleanup() {};
		//
		#pragma endregion 

		[TestMethod]
		void OneChunkMultiply()
		{
			op1->chunks[0] = 0x08;
			op1->chunks[1] = 0x00;
			op1->chunks[2] = 0x00;
			op1->normalized = true;
			op1->size = 3;
			op1->positive = true;

			op2->chunks[0] = 0x01;
			op2->chunks[1] = 0x00;
			op2->chunks[2] = 0x00;
			op2->normalized = true;
			op2->size = 3;
			op2->positive = true;

			result1->chunks[0] = 0;
			result1->chunks[1] = 0;
			result1->chunks[2] = 0;
			result1->size = 0;

			Multiply(op1, op2, result1);
			BigInt::normalize(*result1);
			Assert::AreEqual<BigInt::chunkType>(0x08, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x00, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x0, result1->chunks[2]);
			Assert::AreEqual<int>(1, result1->size);
		};

		[TestMethod]
		void OneChunkMultiplyTwo()
		{
			op1->chunks[0] = 0x10;
			op1->chunks[1] = 0x00;
			op1->chunks[2] = 0x00;
			op1->normalized = true;
			op1->size = 3;
			op1->positive = true;

			op2->chunks[0] = 0x10;
			op2->chunks[1] = 0x00;
			op2->chunks[2] = 0x00;
			op2->normalized = true;
			op2->size = 3;
			op2->positive = true;

			result1->chunks[0] = 0;
			result1->chunks[1] = 0;
			result1->chunks[2] = 0;
			result1->size = 0;

			Multiply(op1, op2, result1);
			BigInt::normalize(*result1);
			Assert::AreEqual<BigInt::chunkType>(0x00, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x02, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x0, result1->chunks[2]);
			Assert::AreEqual<int>(2, result1->size);
		};

		[TestMethod]
		void TwoChunkMultiply()
		{
			op1->chunks[0] = 0x10;
			op1->chunks[1] = 0x10;
			op1->chunks[2] = 0x00;
			op1->normalized = true;
			op1->size = 3;
			op1->positive = true;

			op2->chunks[0] = 0x10;
			op2->chunks[1] = 0x10;
			op2->chunks[2] = 0x00;
			op2->normalized = true;
			op2->size = 3;
			op2->positive = true;

			result1->chunks[0] = 0x00;
			result1->chunks[1] = 0x00;
			result1->chunks[2] = 0x00;
			result1->size = 0;

			Multiply(op1, op2, result1);
			BigInt::normalize(*result1);
			Assert::AreEqual<BigInt::chunkType>(0x00, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x02, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x04, result1->chunks[2]);
			Assert::AreEqual<BigInt::chunkType>(0x02, result1->chunks[3]);
			Assert::AreEqual<int>(4, result1->size);
		};

		[TestMethod]
		void TwoChunkMultiplyComplex()
		{
			op1->chunks[0] = 0x7F;
			op1->chunks[1] = 0x7F;
			op1->chunks[2] = 0x00;
			op1->normalized = true;
			op1->size = 3;
			op1->positive = true;

			op2->chunks[0] = 0x7F;
			op2->chunks[1] = 0x7F;
			op2->chunks[2] = 0x00;
			op2->normalized = true;
			op2->size = 3;
			op2->positive = true;

			result1->chunks[0] = 0x00;
			result1->chunks[1] = 0x00;
			result1->chunks[2] = 0x00;
			result1->size = 0;

			Multiply(op1, op2, result1);
			BigInt::normalize(*result1);
			Assert::AreEqual<BigInt::chunkType>(0x01, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x00, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x7e, result1->chunks[2]);
			Assert::AreEqual<BigInt::chunkType>(0x7f, result1->chunks[3]);
			Assert::AreEqual<int>(4, result1->size);
		};

		[TestMethod]
		void ThreeChunkMultiply()
		{
			op1->chunks[0] = 0x71;
			op1->chunks[1] = 0x54;
			op1->chunks[2] = 0x2e;
			op1->normalized = true;
			op1->size = 3;
			op1->positive = true;

			op2->chunks[0] = 0x13;
			op2->chunks[1] = 0x73;
			op2->chunks[2] = 0x5f;
			op2->normalized = true;
			op2->size = 3;
			op2->positive = true;

			result1->chunks[0] = 0x00;
			result1->chunks[1] = 0x00;
			result1->chunks[2] = 0x00;
			result1->size = 0;

			Multiply(op1, op2, result1);
			BigInt::normalize(*result1);
			Assert::AreEqual<BigInt::chunkType>(0x63, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x0f, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x07, result1->chunks[2]);
			Assert::AreEqual<BigInt::chunkType>(0x7d, result1->chunks[3]);
			Assert::AreEqual<BigInt::chunkType>(0x7a, result1->chunks[4]);
			Assert::AreEqual<BigInt::chunkType>(0x22, result1->chunks[5]);
			Assert::AreEqual<int>(6, result1->size);
		};

		[TestMethod]
		void TestMultiplyDifferingSign() {
			op1->ParseHexadecimalString("2387987", true);
			op2->ParseHexadecimalString("AD198F", true);
			expected->ParseHexadecimalString("18062E1A51169", true);

			// Pos/pos
			APIAL::Multiply(op1, op2, result1);
			Assert::IsTrue(op1->positive);
			Assert::IsTrue(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Debug: verify operands are not being changed
			expected->ParseHexadecimalString("2387987", true);
			Assert::IsTrue(Equal(op1, expected));
			expected->ParseHexadecimalString("AD198F", true);
			Assert::IsTrue(Equal(op2, expected));

			// Neg/neg
			op1->positive = false;
			op2->positive = false;
			expected->ParseHexadecimalString("18062E1A51169", true);
			APIAL::Multiply(op1, op2, result1);
			Assert::IsFalse(op1->positive);
			Assert::IsFalse(op2->positive);
			Assert::IsTrue(result1->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Pos/neg
			op1->positive = true;
			op2->positive = false;
			expected->positive = false;
			APIAL::Multiply(op1, op2, result1);
			Assert::IsTrue(op1->positive);
			Assert::IsFalse(op2->positive);
			Assert::IsFalse(result1->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Neg/pos
			op1->positive = false;
			op2->positive = true;
			APIAL::Multiply(op1, op2, result1);
			Assert::IsFalse(op1->positive);
			Assert::IsTrue(op2->positive);
			Assert::IsFalse(result1->positive);
			Assert::IsTrue(Equal(result1, expected));
		}


	};
}
